package com.hsj.demo.controller;

import com.hsj.demo.entity.Group;
import com.hsj.demo.entity.User;
import com.hsj.demo.service.GroupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@RequestMapping("/group")
public class GroupController {
    @Autowired
    private GroupService groupService;

    @RequestMapping("/findByUserId")
    public List<Group> findByUserId(HttpSession session){
        User user = (User)session.getAttribute("user");
        return groupService.findByUserId(user.getId());
    }

    @RequestMapping("/findGroupNameByUserId")
    public List<Group> findGroupNameByUserId(HttpSession session){
        User user = (User)session.getAttribute("user");
        return groupService.findGroupNameByUserId(user.getId());
    }

    @RequestMapping("/insert")
    public String insert(Group group,HttpSession session){
        User user = (User)session.getAttribute("user");
        return groupService.insert(group,user);
    }

    @RequestMapping("/findByKeyword")
    public List<Group> findByKeyword(Group group,HttpSession session) {
        User user = (User)session.getAttribute("user");
        return groupService.findByKeyword(group,user);
    }

    @RequestMapping("/update")
    public String update(Group group,HttpSession session){
        User user = (User)session.getAttribute("user");
        return groupService.update(group,user);
    }

    @RequestMapping("/deleteById")
    public String deleteById(Integer id){
        return groupService.deleteByGroupId(id);
    }
}
